#!/bin/bash

# # 执行入口 
# # 进入脚本目录位置
# cd `dirname $0`
# # 获取当前脚本目录位置
# shellfilepath=`pwd`'/'$0
# # 打印脚本目录地址
# echo "[INFO]: 脚本地址 "${shellfilepath}


# 设定壁纸目录路径：
#DIR=/usr/share/backgrounds
#DIR=/home/whoami/Pictures/wallpapers
DIR=$HOME/Pictures/wallpapers

# 设定切换桌面背景的时间间隔，单位为'秒'：
SEC=30


# 判断桌面环境
# if [ "$DESKTOP_SESSION" == "gnome" ] || [ "$XDG_CURRENT_DESKTOP" == "GNOME" ] ; then 
# 	echo "当前是gnome桌面";
# else
# 	echo "当前不是gnome桌面";
# fi

# 判断脚本是否已经再执行
#sh_process=`ps -aux|grep wallpaper-autochange-gnome.sh| grep -v grep|grep -v PPID|awk '{print $2}'`
sh_process=`pidof -x "wallpaper-autochange-gnome.sh"`
for i in $sh_process
do
	# echo $i
	# echo $$
	if [ $i == $$ ] # 当前进程则跳过
	then
		continue
	fi
	# 非当前则，杀死
	kill -9 $i
done

# if pidof -x "wallpaper-autochange-gnome.sh" >/dev/null; then
# 	# 脚本已有在执行
# 	echo "Process already running"
# 	# 杀死正在执行的脚本进行
# else
# 	# 脚本未在执行
# 	echo "Process not running"

# fi

# 进入一个死循环
while true
do
	# 随机取文件夹下的一个文件
	# echo $(ls $DIR/* | shuf -n1)
	#WALLPAPER_PIC=$(ls $DIR/*.jpg | shuf -n1)
	WALLPAPER_PIC=$(ls $DIR/* | shuf -n1)
	# 设置壁纸
	#gsettings set org.gnome.desktop.background picture-uri 'file:///home/whoami/Pictures/wallpapers/wallhaven-0jy6wp.jpg'
	gsettings set org.gnome.desktop.background picture-uri 'file://$WALLPAPER_PIC'
	cmdStr="gsettings set org.gnome.desktop.background picture-uri  'file://$WALLPAPER_PIC'"
	# echo "执行命令: ${cmdStr} "
	${cmdStr}

	# 休息三分钟
	#sleep 3m
	# 休息三秒
	#sleep 3s
	sleep "$SEC"
done

